<?php

function transform($dom, $templates)
{
    $this_dir = dirname(__FILE__);
    $ds = DIRECTORY_SEPARATOR;

    foreach ($templates as $xslt) {
        $processor = new XSLTProcessor();
        $processor->importStyleSheet(DOMDocument::loadXML(file_get_contents($this_dir.$ds.$xslt.'.xsl')));
        $dom = $processor->transformToDoc($dom);
    }

    return $dom;
}

$this_dir = dirname(__FILE__);
$ds = DIRECTORY_SEPARATOR;

$source = 'grammar';
$source = DOMDocument::loadXML(file_get_contents($this_dir.$ds.$source.'.xml'));

$prepared = transform($source, array(
    'apply-default-values',
    'counte-terminals',
));

$sets = rtrim(transform($prepared, array(
    'build-sets',
    'sets-text',
    'xml2text',
))->firstChild->nodeValue);

$table = rtrim(transform($prepared, array(
    'build-metasets',
    'to_table',
    'xml2text',
))->firstChild->nodeValue);

highlight_string("<?php\n\n\$sets = {$sets};\n\n\$table = {$table};\n\n?>");

?>